home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / test / lib / pipe.c < prev    next >
C/C++ Source or Header  |  1992-11-23  |  922b  |  61 lines

  1.  
  2. /*
  3.  *  x prog args
  4.  */
  5.  
  6. #include <sys/types.h>
  7. #include <sys/time.h>
  8. #include <sys/resource.h>
  9. #include <sys/wait.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12.  
  13. main(ac, av)
  14. char *av[];
  15. {
  16.     union wait istat;
  17.     long pid;
  18.     int fds[2];
  19.     short j;
  20.  
  21.     for (j = 1; j < ac; ++j) {
  22.     if (av[j][0] == '|') {
  23.         av[j] = NULL;
  24.         break;
  25.     }
  26.     }
  27.  
  28.     if (j == ac) {
  29.     startproc(av + 1, -1, -1);
  30.     } else {
  31.     pipe(fds);
  32.     startproc(av + 1, -1, fds[1]);
  33.     startproc(av + j + 1, fds[0], -1);
  34.     }
  35.     while ((pid = wait(&istat)) != -1)
  36.     printf("PID %d code %d\n", pid, istat.w_retcode);
  37.     return(0);
  38. }
  39.  
  40. int
  41. startproc(av, infd, outfd)
  42. char **av;
  43. int infd;
  44. int outfd;
  45. {
  46.     int pid;
  47.  
  48.     if ((pid = amiga_vfork()) == 0) {
  49.     printf("child\n");
  50.     if (infd >= 0)
  51.         amiga_dup2(infd, 0);
  52.     if (outfd >= 0)
  53.         amiga_dup2(outfd, 1);
  54.     amiga_execvp(av[0], av);
  55.     } else if (pid < 0) {
  56.     puts("vfork failed");
  57.     }
  58.     return(0);
  59. }
  60.  
  61.